package com.ayou.eggguard.farm.service.impl;

import com.ayou.common.utils.PageBean;
import com.ayou.common.utils.ThreadLocalUtil;
import com.ayou.eggguard.farm.mapper.AnnounceMapper;
import com.ayou.eggguard.farm.pojo.Announce;
import com.ayou.eggguard.farm.service.AnnounceService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Service
public class AnnounceServiceImpl implements AnnounceService {

    @Autowired
    private AnnounceMapper announceMapper;

    @Override
    public void add(Announce announce) {
        Map<String,Object> map= ThreadLocalUtil.get();
        String username= (String) map.get("username");
        announce.setUsername(username);
        announce.setCreateTime(LocalDateTime.now());
        announceMapper.add(announce);
    }

    @Override
    public void delete(Integer id) {
        announceMapper.delete(id);
    }

    @Override
    public void updateAnnounce(Announce announce) {
        announceMapper.updateAnnounce(announce);
    }

    @Override
    public PageBean<Announce> getAnnounceByPage(Integer pageNum, Integer pageSize, String title) {
        PageBean<Announce> pb=new PageBean<>();
        PageHelper.startPage(pageNum,pageSize);
        List<Announce> list=announceMapper.getAnnounceByPage(title);
        Page<Announce> p= (Page<Announce>) list;
        pb.setTotal(p.getTotal());
        pb.setItems(p.getResult());
        return pb;
    }

    @Override
    public void updateAnnounceStatus(Integer id) {

        announceMapper.updateAnnounceStatus(id);
    }
}
